<?php

class HelpCreateController extends BaseController
{
	
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Help;
		//数据验证
		$this->performAjaxValidation($model);

		if(isset($_POST['Help']))
		{
			$model->attributes=$_POST['Help'];
			if($_POST['cateid3']>0){
				$model->zhc_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->zhc_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->zhc_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'请选择类目！'));
				exit;
			}
			
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'恭喜，帮助信息发布成功',
						'links'=>array(
								array('继续发布帮助信息',$this->pre_module_url.'/helpCreate/create'),
								array('帮助信息管理',$this->pre_module_url.'/help/admin'),
								array('前台浏览帮助信息','/home/help/detail/id/'.$model->zhe_id),
						),
				));
				exit;
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
 	 * ajax验证
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='help-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}